package com.qiqidream.admin.security.api;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.qiqidream.admin.common.base.BaseController;
import com.qiqidream.admin.common.constant.StaticConstants;
import com.qiqidream.admin.common.enums.CaptchaType;
import com.qiqidream.admin.security.entity.CaptchaVo;
import com.qiqidream.admin.tools.model.entity.SysSetting;
import com.qiqidream.admin.tools.model.po.CaptchaSetting;
import com.qiqidream.admin.tools.service.SysSettingService;
import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 首页
 *
 * @author QiQiDream
 * @since 2019/11/18 10:53
 */
@RestController
@Api(tags = "登录页-接口")
@RequestMapping("/login")
public class IndexController extends BaseController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private SysSettingService sysSettingService;

    @ApiOperation(value = "获取验证码")
    @GetMapping("/captcha")
    public CaptchaVo captcha(String uuid) {
        SysSetting sysSetting = sysSettingService.getOne(new LambdaQueryWrapper<SysSetting>().eq(SysSetting::getCode, "captcha"));

        if (!sysSetting.getEnabled()) {
            throw new ApiException("验证码未开启");
        }


        CaptchaSetting captchaSetting = JSON.toJavaObject(sysSetting.getConfig(), CaptchaSetting.class);

        Captcha captcha;

        switch (captchaSetting.getType()) {
            case 1:
                captcha = new SpecCaptcha(135, 52, captchaSetting.getLength());
                break;
            case 2:
                captcha = new GifCaptcha(135, 52, captchaSetting.getLength());
                break;
            case 3:
                captcha = new ChineseCaptcha(135, 52, captchaSetting.getLength());
                break;
            case 4:
                captcha = new ChineseGifCaptcha(135, 52, captchaSetting.getLength());
                break;
            default:
                captcha = new ArithmeticCaptcha(135, 52, captchaSetting.getLength());
                break;
        }
        String key = ObjectUtils.isEmpty(uuid) ? IdUtil.fastSimpleUUID() : uuid;

        // 存入redis并设置过期时间为5分钟
        stringRedisTemplate.opsForValue().set(StaticConstants.REDIS_CAPTCHA_KEY_PREFIX + key, captchaSetting.getType().equals(CaptchaType.ARITHMETIC_CAPTCHA.getCode()) ? captcha.text() : captcha.text().toLowerCase(), 5, TimeUnit.MINUTES);
        CaptchaVo captchaVo = new CaptchaVo();
        captchaVo.setCaptcha(captcha.toBase64());
        captchaVo.setUuid(key);
        return captchaVo;
    }

}
